× Lesson 1 Lesson 2 Lesson 3 Lesson 4 Lesson 5 Lesson 6 Lesson 7 Lesson 8 Lesson 9 Lesson 10 Lesson 11 Lesson 12 Lesson 13 Lesson 14 Lesson 15 Lesson 16 Lesson 17 Lesson 18 Lesson 19 Lesson 20 Lesson 21 Lesson 22 Lesson 23 Lesson 24 Lesson 25 Mini Lesson 1 Mini Lesson 2 Mini Lesson 3 Mini Lesson 4 Mini Lesson 5

Lessons

Lesson 20 - Files

Files used with Python can be very useful if you want to save what you are outputting into a file, rather than it just being outputed to the console. You can use Python to create, edit, and read files. Here is the basic syntax for creating a file:

file = open('hello_world.txt', 'w') file.write('Hello World!') file.close()

There is a lot to unpack here. One the first line, a variable named file was set equal to the function open(), where the two arguments 'hello_world.txt' and 'w', and note that I have the 'file.' in front of the open() function. Since we do not have a text file called 'hello_world.txt', Python created a file just for us. If there was already a file called 'hello_world.txt', then we would be deleting the old one and starting over. The 'w' means that we are writing the file, rather than reading it, which will be discussed later on. On the third line, we use the file variable name and the close() function, which is required whenever working with files. Note, if you were to do two write(), they would be on the same line. To counteract that, simply use write('\n') in between the stuff you want to write in the file, as '\n' always goes to the next line. Let us see what happens when read this file:

file = open('hello_world.txt', 'r') for x in file: print(x) file.close()

On the first line, we do exactly the same for when we were writing the file, except we have a 'r' instead of a 'w'. On the second line, we start our for loop, and we are iterating through the file variable. On the third line we are simply printing out each line of the file, and since there is only one line in our file currently, it will print out one line. Fourth line, making sure to always close().

If you want to edit a file rather than totally replace it, simply replace 'w' with 'a'. Here's an example:

file = open('hello_world.txt', 'a') file.write('\n') file.write('Hello World again!') file.close()

If we were to print out the file, this time it would print out 'Hello World!' on one line and 'Hello World again!' on the next line.